//
//  UIColorTest.swift
//  EasySwift
//
//  Created by 王隆帅 on 2017/6/9.
//  Copyright © 2017年 王隆帅. All rights reserved.
//

import UIKit
import CoreGraphics

class UIColorTest: UIViewController {

    var viewIndex = 0;

    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white;
        // Do any additional setup after loading the view.
        
        self.testLiteralInit();
    }
    
    // 测试字面量初始化方法
    func testLiteralInit() {
        
        self.addImageView(color: #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1));
    }
    
    // 测试获取color的相关信息
    func testGetInfomationWithColor() {
        
        var r: CGFloat = 0;
        var g: CGFloat = 0;
        var b: CGFloat = 0;
        var a: CGFloat = 0;
        
        UIColor.red.getRed(&r, green: &g, blue: &b, alpha: &a);
        print("RGB分量---" + "r:" + String(describing: r) + " g:" + String(describing: g) + " b:" + String(describing: b) + " a:" + String(describing: a));
        
        
        var hh: CGFloat = 0;
        var ss: CGFloat = 0;
        var bb: CGFloat = 0;
        var aa: CGFloat = 0;
        
        UIColor.red.getHue(&hh, saturation: &ss, brightness: &bb, alpha: &aa);
        
        print("HSB分量---" + "hh:" + String(describing: hh) + " ss:" + String(describing: ss) + " bb:" + String(describing: bb) + " aa:" + String(describing: aa));
        
        
        var www: CGFloat = 0;
        var aaa: CGFloat = 0;
        
        UIColor.red.getWhite(&www, alpha: &aaa);
        
        print("灰度分量---" + "www:" + String(describing: www) + " aaa:" + String(describing: aaa));
        
        
        print("cgcolor---" + String(describing: UIColor.red.cgColor));
        
        print("cicolor---" + String(describing: UIColor.init(ciColor: CIColor.red()).ciColor));
    }
    
    
    // 测试设置图形上下文的绘图颜色
    func testDrawColor() {
        
        let pathView = PathView.init(frame: self.view.bounds);
        self.view.addSubview(pathView);
    }
    
    // 测试 image -> color
    func imageToColor() {
        
        let color = UIColor.init(patternImage: UIImage.init(named: "imageToColor")!)
        let imageView = UIImageView.init(frame: CGRect.init(x: 40, y: 80, width: 158, height: 154));
        imageView.backgroundColor = color;
        self.view.addSubview(imageView);
        print(color);
    }
    
    // 测试 color to color
    func testColorToColor() {
        
        // CGColor -> UIColor
        self.addImageView(color: UIColor.init(cgColor: CGColor.init(colorSpace: CGColorSpaceCreateDeviceCMYK(), components: [1,1,0,0,1])!));
        
        // CIColor -> UIColor
        self.addImageView(color: UIColor.init(ciColor: CIColor.gray()));
        
        // UIColor ->UIColor(+alpha)
        self.addImageView(color: UIColor.red.withAlphaComponent(0.5));
    }
    
    // 测试在特定颜色空间创建UIColor
    func testCreateColorWithSpecialColorSpace() {
        
        self.addImageView(color: UIColor.init(white: 0.3, alpha: 1.0));
        self.addImageView(color: UIColor.init(hue: 0.3, saturation: 0.5, brightness: 0.7, alpha: 1.0));
        self.addImageView(color: UIColor.init(red: 0.3, green: 0.5, blue: 0.7, alpha: 1.0));
        self.addImageView(color: UIColor.init(displayP3Red: 0.3, green: 0.5, blue: 0.7, alpha: 1.0));
    }
    
    // 测试使用预定义颜色创建颜色对象
    func testUsePreColorCreateColor() {
        
        // 颜色空间：kCGColorSpaceExtendedGray
        self.addImageView(color: UIColor.black);
        self.addImageView(color: UIColor.darkGray);
        self.addImageView(color: UIColor.lightGray);
        self.addImageView(color: UIColor.white);
        self.addImageView(color: UIColor.gray);
        self.addImageView(color: UIColor.clear);

        // 颜色空间：kCGColorSpaceExtendedSRGB
        self.addImageView(color: UIColor.red);
        self.addImageView(color: UIColor.green);
        self.addImageView(color: UIColor.blue);
        self.addImageView(color: UIColor.cyan);
        self.addImageView(color: UIColor.yellow);
        self.addImageView(color: UIColor.magenta);
        self.addImageView(color: UIColor.orange);
        self.addImageView(color: UIColor.purple);
        self.addImageView(color: UIColor.brown);
    }
    
    // 测试创建系统颜色
    func testCreateSystemColor() {
        
        // 颜色空间：kCGColorSpaceExtendedGray
        self.addImageView(color: UIColor.darkText);
        self.addImageView(color: UIColor.lightText);
        
        // 颜色空间：kCGColorSpaceExtendedSRGB
        self.addImageView(color: UIColor.groupTableViewBackground);
    }
    
    // 添加imageView
    func addImageView(color: UIColor) {
        
        let imageView = UIImageView.init(frame: CGRect.init(x: 40, y: 80 + self.viewIndex * 30, width: 200, height: 20));
        imageView.backgroundColor = color;
        self.view.addSubview(imageView);
        print(color);
        
        print(color.cgColor.colorSpace ?? "没有颜色空间");
        
        self.viewIndex += 1;
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
